/**
 * 
 */
package br.org.bertol.mestrado;

import java.util.concurrent.CountDownLatch;

import br.org.bertol.mestrado.engine.EngineAnt;
import br.org.bertol.mestrado.engine.fitness.Objective;
import br.org.bertol.mestrado.util.AntConfiguration;
import br.org.bertol.mestrado.util.Configurator;

/**
 * @author contaqualquer
 */
public class StarterAnt extends AbstractStarter<EngineAnt> {

    /**
     * @param configurator
     *            Portador das mensagens
     */
    public StarterAnt(final Configurator configurator) {
        super(configurator);
    }

    /**
     * @exception InterruptedException
     *                Erro na thread
     */
    public final void startEngine() throws InterruptedException {
        startAntEngineBySystem(configurator
                .getSourcePath());
    }

    /**
     * @param sourePath
     *            Caminho para salvar o arquivo
     * @throws InterruptedException
     *             Erro na thread
     */
    public final void startAntEngineBySystem(final String sourePath) throws InterruptedException {
        for (String system : configurator.getSystems()) {

            final AntConfiguration antConfiguration = configurator
                    .loadAntConfiguration(system);

            startAntEngineByInput(sourePath, system, antConfiguration);
        }
    }

    /**
     * @param sourePath
     *            Caminho para salvar arquivo
     * @param system
     *            Sistema sendo avalliado
     * @param antConfig
     *            TODO
     * @throws InterruptedException
     *             Erro nas threads
     */
    public final void startAntEngineByInput(final String sourePath,
            final String system, final AntConfiguration antConfig)
            throws InterruptedException {

        for (Objective[] objectives : configurator.getObjectives()) {
            startAntEngine(objectives, sourePath,
                           system, antConfig);
        }
    }

    /**
     * @param constraints
     *            TODO
     * @param sourePath
     *            TODO
     * @param system
     *            TODO
     * @param antConfig
     *            TODO
     * @throws InterruptedException
     *             TODO
     */
    public final void startAntEngine(final Objective[] constraints,
            final String sourePath,
            final String system, final AntConfiguration antConfig)
            throws InterruptedException {

        final CountDownLatch systemstart = new CountDownLatch(1);

        final CountDownLatch systemstop = new CountDownLatch(1);

        final EngineAnt engineAnt = new EngineAnt(systemstart, systemstop,
                constraints, sourePath, system, antConfig);

        engineAnt.start();

        // notifica threads que
        // buscas podem iniciar
        systemstart.countDown();

        // coloca sistema em modo de espera
        // pela conclusão das buscas
        systemstop.await();

        // adiciona na lista de soluções
        engineList.add(engineAnt);

    }
}
